package com.qkl.xiatang.app.service.impl;

import com.qkl.xiatang.app.cache.LocalCacheBuilder;
import com.qkl.xiatang.app.cache.LocalSimpleCache;
import com.qkl.xiatang.app.dto.message.MessagePushUserDTO;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.HttpResponse;
import com.qkl.xiatang.app.utils.HttpUtils;
import com.qkl.xiatang.app.utils.appenum.MessageTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MessagePushService {

    private static final String URL = "https://fc-mp-159e3b75-b8df-446c-a0f3-3469a992f5f3.next.bspapp.com/breed";

    private static final String DATA_URL = "/pages/tabbar/message/messageDetails";

    @Autowired
    private ConfigService configService;


    private LocalSimpleCache<String> urlCache;
    private LocalSimpleCache<String> dataUrlCache;
    @PostConstruct
    public void initCache(){
        LocalCacheBuilder<String> urlCacheBuilder = new LocalCacheBuilder<>();
        urlCache = urlCacheBuilder.build(60,null,()-> StringUtils.trimToEmpty(configService.messagePushUrl()));

        LocalCacheBuilder<String> dataUrlCacheBuilder = new LocalCacheBuilder<>();
        dataUrlCache = dataUrlCacheBuilder.build(60,null,()-> StringUtils.trimToEmpty(configService.messagePushDataUrl()));

    }


    public void pushMessage(String url, List<String> cids, String title, String content, String dataUrl){
        Map<String,Object> data = new HashMap<>();
        data.put("url",dataUrl);

        Map<String,Object> params = new HashMap<>();
        params.put("cids",cids);
        params.put("title",title);
        params.put("content",content);
        params.put("data",data);
        params.put("request_id", AppUtils.genIdStr());
        HttpResponse<String>  response = HttpUtils.postRequestJson(url,params);
        log.info("===pushMessage response={}",response);
    }

    public void pushMessage(List<String>  cids,String title,String content,String messageId){
        String url = urlCache.getCache();
        String dataUrl = dataUrlCache.getCache();
        if(StringUtils.isEmpty(url)){
            url = URL;
        }
        if(StringUtils.isEmpty(dataUrl)){
            dataUrl = DATA_URL;
        }
        dataUrl = dataUrl + "?id=" + messageId;
        this.pushMessage(url,cids,title,content,dataUrl);
    }
    public void pushMessage(String title, String content, MessagePushUserDTO pushUserDTO, MessageTypeEnum typeEnum){
        log.info("==== pushUserDTO={}",pushUserDTO);
        if (null == pushUserDTO) {
            log.info("==== pushMessage === 无推送用户");
            return;
        }
        List<String> cids = pushUserDTO.getCids(typeEnum);
        if (ObjectUtils.isEmpty(cids)) {
            log.info("==== pushMessage === 无推送用户设备");
            return;
        }
        try {
            this.pushMessage(cids, title, content,pushUserDTO.getMessageId());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

